package com.xinke.edu.mypetadoption.Activity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.xinke.edu.mypetadoption.R;
import com.xinke.edu.mypetadoption.databinding.ActivityProductDetailBinding;
import com.xinke.edu.mypetadoption.entity.Product;
import com.xinke.edu.mypetadoption.util.ShoppingCartManager;

public class ProductDetailActivity extends AppCompatActivity {

    private ImageView btnBack;
    private ImageView ivProductImage;
    private TextView tvProductTitle;
    private TextView tvProductPrice;
    private TextView tvProductDescription;
    private androidx.appcompat.widget.AppCompatButton btnDecrease;
    private androidx.appcompat.widget.AppCompatButton btnIncrease;
    private EditText etQuantity;
    private androidx.appcompat.widget.AppCompatButton btnAddToCart;
    private androidx.appcompat.widget.AppCompatButton btnBuyNow;
    
    private int quantity = 1;
    private Product currentProduct;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_product_detail);
        
        initViews();
        initListeners();
        
        // 从Intent获取商品对象
        Product product = (Product) getIntent().getSerializableExtra("product");
        if (product != null) {
            // 设置商品信息
            setProductInfo(product);
        } else {
            Toast.makeText(this, "商品信息加载失败", Toast.LENGTH_SHORT).show();
        }
    }
    
    private void initViews() {
        btnBack = findViewById(R.id.btn_back);
        ivProductImage = findViewById(R.id.iv_product_image);
        tvProductTitle = findViewById(R.id.tv_product_title);
        tvProductPrice = findViewById(R.id.tv_product_price);
        tvProductDescription = findViewById(R.id.tv_product_description);
        btnDecrease = findViewById(R.id.btn_decrease);
        btnIncrease = findViewById(R.id.btn_increase);
        etQuantity = findViewById(R.id.et_quantity);
        btnAddToCart = findViewById(R.id.btn_add_to_cart);
        btnBuyNow = findViewById(R.id.btn_buy_now);
        
        // 初始化数量显示
        etQuantity.setText(String.valueOf(quantity));
    }
    
    private void initListeners() {
        // 返回按钮
        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        
        // 减少数量
        btnDecrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (quantity > 1) {
                    quantity--;
                    etQuantity.setText(String.valueOf(quantity));
                }
            }
        });
        
        // 增加数量
        btnIncrease.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                quantity++;
                etQuantity.setText(String.valueOf(quantity));
            }
        });
        
        // 数量输入监听
        etQuantity.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    try {
                        int inputQuantity = Integer.parseInt(etQuantity.getText().toString());
                        if (inputQuantity < 1) {
                            quantity = 1;
                            etQuantity.setText("1");
                        } else {
                            quantity = inputQuantity;
                        }
                    } catch (NumberFormatException e) {
                        quantity = 1;
                        etQuantity.setText("1");
                    }
                }
            }
        });
        
        // 加入购物车
        btnAddToCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addToCart();
            }
        });
        
        // 立即购买
        btnBuyNow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buyNow();
            }
        });
    }
    
    private void setProductInfo(Product product) {
        this.currentProduct = product;
        // 设置商品标题
        tvProductTitle.setText(product.getName());
        
        // 设置商品价格
        tvProductPrice.setText("￥" + product.getPrice());
        
        // 设置商品描述
        if (product.getDescription() != null) {
            tvProductDescription.setText(product.getDescription());
        }
        
        // 加载商品图片
        if (product.getCoverImage() != null && !product.getCoverImage().isEmpty()) {
            Glide.with(this).load(product.getCoverImage()).into(ivProductImage);
        } else {
            ivProductImage.setImageResource(R.mipmap.ic_launcher);
        }
    }
    
    private void addToCart() {
        if (currentProduct != null) {
            // 使用购物车管理器添加商品
            ShoppingCartManager.getInstance().addItem(
                    currentProduct.getName(),
                    currentProduct.getPrice().doubleValue(),
                    quantity,
                    currentProduct.getCoverImage() != null ? currentProduct.getCoverImage() : "",
                    (currentProduct.getId() != null) ? currentProduct.getId().intValue() : 0
            );
            Toast.makeText(this, "已加入购物车 x" + quantity, Toast.LENGTH_SHORT).show();
        }
    }
    
    private void buyNow() {
        // 立即购买逻辑
        Toast.makeText(this, "立即购买 " + quantity + " 件", Toast.LENGTH_SHORT).show();
        finish();
    }
}
